package src.first;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ColorAction extends JFrame{

    private JPanel contentPane;


    private class color implements ActionListener{

        private Color background;

        public color(Color background) {
            this.background = background;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            contentPane.setBackground(background);
        }
    }



    public ColorAction(){
        setTitle("普通内部类的简单应用");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 500, 200);
        contentPane = new JPanel();
        contentPane.setLayout(null);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);

        final JButton redButton = new JButton();
        redButton.setText("红色");
        redButton.setBounds(15, 20, 82, 30);
        redButton.addActionListener(new color(Color.RED));
        contentPane.add(redButton);

        final JButton greenButton = new JButton();
        greenButton.setText("绿色");
        greenButton.setBounds(100, 20, 82, 30);
        greenButton.addActionListener(new color(Color.GREEN));
        contentPane.add(greenButton);

        final JButton blueButton = new JButton();
        blueButton.setText("蓝色");
        blueButton.setBounds(185, 20, 82, 30);
        blueButton.addActionListener(new color(Color.BLUE));
        contentPane.add(blueButton);

    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try{
                    ColorAction colorAction = new ColorAction();
                    colorAction.setVisible(true);
                    colorAction.contentPane.requestFocus();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
    }
}
